#!/bin/bash

# 域名
domain='localhost'

# MySQL管理员密码
mysql_root_pwd='123456'

# 管理员邮箱
admin_email='ahhbgzg@126.com'

# 配置系统环境
yum remove -y httpd httpd-devel httpd-manual httpd-tools php php-cli php-common php-gd php-ldap php-mysql php-odbc php-pdo php-pear php-pecl-apc php-pecl-memcache php-pgsql php-soap php-xml php-xmlrpc mysql mysql-bench mysql-connector-java mysql-connector-odbc mysql-devel mysql-server mysql-test
yum install -y gcc gcc-c++ make wget screen iptables sendmail mailx perl perl-devel pcre pcre-devel zlib zlib-devel openssl openssl-devel cmake autoconf automake zip unzip bison ncurses ncurses-devel curl curl-devel libtool libxml2 libxml2-devel libxslt libevent bzip2 bzip2-devel gd libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel readline readline-devel flex

# 检查文件
if [ ! -s 'mysql-5.5.40.tar.gz' ]; then
wget http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.40.tar.gz
fi

if [ ! -s 'httpd-2.4.10.tar.gz' ]; then
wget http://archive.apache.org/dist/httpd/httpd-2.4.10.tar.gz
fi

if [ ! -s 'apr-1.5.0.tar.gz' ]; then
wget http://archive.apache.org/dist/apr/apr-1.5.0.tar.gz
fi

if [ ! -s 'apr-util-1.5.3.tar.gz' ]; then
wget http://archive.apache.org/dist/apr/apr-util-1.5.3.tar.gz
fi

if [ ! -s 'php-5.4.34.tar.gz' ]; then
wget http://us1.php.net/distributions/php-5.4.34.tar.gz
fi

if [ ! -s 'libmcrypt-2.5.8.tar.gz' ]; then
wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
fi

if [ ! -s 'mhash-0.9.9.9.tar.gz' ]; then
wget http://nchc.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
fi

if [ ! -s 'mcrypt-2.6.8.tar.gz' ]; then
wget http://nchc.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
fi

if [ ! -s 'APC-3.1.13.tgz' ]; then
wget http://pecl.php.net/get/APC-3.1.13.tgz
fi

if [ ! -s 'memcache-2.2.7.tgz' ]; then
wget http://pecl.php.net/get/memcache-2.2.7.tgz
fi

# 备份配置文件
if [ ! -d 'conf_backup'  ];then
mkdir 'conf_backup'
fi

if [ -f '/etc/my.cnf' ]; then
mv /etc/my.cnf conf_backup
fi

if [ -f '/etc/init.d/mysql' ]; then
mv /etc/init.d/mysql conf_backup
fi

if [ -f '/etc/init.d/httpd' ]; then
mv /etc/init.d/httpd conf_backup
fi

if [ -f '/etc/httpd.conf' ]; then
mv /etc/httpd.conf conf_backup
fi

if [ -f '/etc/php.ini' ]; then
mv /etc/php.ini conf_backup
fi


####################################### 开始安装MySQL #######################################


# 开始安装MySQL
tar zxvf mysql-5.5.40.tar.gz
cd mysql-5.5.40/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1
make && make install

# MySQL配置文件

# my-small.cnf   < 64M
# my-medium.cnf  32M ~ 64M
# my-large.cnf   512M
# my-huge.cnf    1G ~ 2G

cp support-files/my-huge.cnf /etc/my.cnf

# 开启Innodb支持
sed -i 's:#innodb:innodb:g' /etc/my.cnf

# 关闭网络连接
sed -i 's:#skip-networking:skip-networking:g' /etc/my.cnf

# 安装数据库
useradd -s /sbin/nologin mysql
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
chown -R mysql.mysql /usr/local/mysql

# 把MySQL注册为服务
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
cd ..

cat > /etc/ld.so.conf.d/mysql.conf<<EOF
/usr/local/mysql/lib
/usr/local/lib
EOF

ldconfig

ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/mysqladmin
ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
ln -s /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe

service mysqld start

# 设置MySQL的管理员密码
mysqladmin -u root password $mysql_root_pwd

# 设置MySQL开机启动
chkconfig mysqld --add
chkconfig mysqld on


####################################### 开始安装Apache #######################################


# 解压
tar zxvf httpd-2.4.10.tar.gz
tar zxvf apr-1.5.0.tar.gz
tar zxvf apr-util-1.5.3.tar.gz
mv apr-1.5.0 httpd-2.4.10/srclib/apr
mv apr-util-1.5.3 httpd-2.4.10/srclib/apr-util

# 编译安装
cd httpd-2.4.10
./configure \
--prefix=/usr/local/apache \
--enable-so --enable-mime-magic --enable-rewrite --enable-deflate --enable-ssl=shared \
--with-included-apr --with-mpm=prefork \
--disable-autoindex --disable-status --disable-env --disable-alias --disable-version
make && make install
cd ..

# 把Apache注册为服务
cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
sed -i '2c\# chkconfig: - 85 15' /etc/init.d/httpd
sed -i '3c\# description: apache' /etc/init.d/httpd

# 修改Apache配置文件
sed -i 's/^#ServerName www.example.com:80/ServerName localhost:80/' /usr/local/apache/conf/httpd.conf
sed -i 's/^#LoadModule expires_module/LoadModule expires_module/' /usr/local/apache/conf/httpd.conf
sed -i 's/^#LoadModule rewrite_module/LoadModule rewrite_module/' /usr/local/apache/conf/httpd.conf
sed -i 's/^#LoadModule deflate_module/LoadModule deflate_module/' /usr/local/apache/conf/httpd.conf
sed -i 's/^#Include conf\/extra\/httpd-vhosts.conf/Include conf\/extra\/httpd-vhosts.conf/' /usr/local/apache/conf/httpd.conf
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html index.htm/g' /usr/local/apache/conf/httpd.conf

# 配置虚拟主机
mkdir /www/html -p
cat > /usr/local/apache/conf/extra/httpd-vhosts.conf<<EOF

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs"
    ServerName localhost
</VirtualHost>

<Directory "/www/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<VirtualHost *:80>
    DocumentRoot "/www/html"
    ServerName ${domain}
    ErrorLog "logs/${domain}-error_log"
    CustomLog "logs/${domain}-access_log" common
</VirtualHost>

EOF

# Apache安全设置
echo "ServerTokens Prod" >> /usr/local/apache/conf/httpd.conf
echo "TraceEnable off" >> /usr/local/apache/conf/httpd.conf

# 创建Apache配置文件快捷方式
ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf
ln -s /usr/local/apache/conf/extra/httpd-vhosts.conf /etc/vhosts.conf

# 设置Apache开机启动
chkconfig httpd --add
chkconfig httpd on

# 设置防火墙开启80端口
if [ -z "`/sbin/iptables -nL | grep dpt:80`" ];then
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart
fi


####################################### 开始安装PHP #######################################


# 安装libmcrypt
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make && make install
ldconfig
cd libltdl/
./configure --enable-ltdl-install
make && make install
cd ../../

# 安装mhash
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make && make install
cd ../

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

ldconfig

# 安装mcrypt
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
./configure
make && make install
cd ../

# 开始安装PHP
tar zxvf php-5.4.34.tar.gz
cd php-5.4.34
./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-bz2 --with-curl --with-gd --with-gettext --with-openssl --with-pear --with-readline --with-xmlrpc --with-zlib \
--with-freetype-dir --with-iconv-dir --with-jpeg-dir --with-libxml-dir --with-png-dir \
--with-mysql --with-mysqli --with-pdo-mysql --with-pdo-sqlite \
--with-mcrypt --with-mhash \
--enable-bcmath --enable-calendar --enable-exif --enable-fpm --enable-ftp --enable-mbstring --enable-mbregex --enable-pcntl --enable-pdo --enable-shmop --enable-soap --enable-sockets --enable-wddx --enable-xml --enable-zip \
--enable-gd-native-ttf --enable-inline-optimization \
--disable-rpath
make && make install

# 添加PHP常用命令
ln -s /usr/local/php/bin/php /usr/bin/php
ln -s /usr/local/php/bin/phpize /usr/bin/phpize

# 配置Apache支持PHP
cp php.ini-production /usr/local/php/lib/php.ini
echo "AddType application/x-httpd-php .php" >> /usr/local/apache/conf/httpd.conf
cd ..

# 安装APC
tar zxvf APC-3.1.13.tgz
cd APC-3.1.13
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-apc --enable-mmap --enable-apc-spinlocks --disable-apc-pthreadmutex
make && make install
cd ..

# 安装memcache扩展
tar zxvf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
cd ..

cat >> /usr/local/php/lib/php.ini <<EOF

[APC]
extension = apc.so
apc.cache_by_default = on
apc.enabled = on
apc.max_file_size = 5m
apc.shm_segments = 1
apc.shm_size = 32M
apc.stat = on
apc.write_lock = On

[Memcache]
extension = memcache.so

EOF

# 修改PHP配置文件
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"#' /usr/local/php/lib/php.ini
sed -i 's/post_max_size = 8M/post_max_size = 50M/g' /usr/local/php/lib/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/g' /usr/local/php/lib/php.ini
sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/lib/php.ini
sed -i 's/expose_php = On/expose_php = Off/g' /usr/local/php/lib/php.ini
sed -i 's/disable_functions =.*/disable_functions = phpinfo,passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/lib/php.ini

# 创建PHP配置文件快捷方式
ln -s /usr/local/php/lib/php.ini /etc/php.ini

# 清理文件
rm -rf libmcrypt-2.5.8
rm -rf mhash-0.9.9.9
rm -rf mcrypt-2.6.8


####################################### 安装完成 #######################################


# 启动Apache
service httpd start


####################################### 安装结束 #######################################

echo "Lamp Install Success!" | mailx -s "The Lamp is ok" $admin_email

echo "LAMP Install Success!Enjoy It!"

